package com.itzhouwei.commerce.advice;

import com.itzhouwei.commerce.vo.CommonResponse;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常捕获处理
 * @description:
 * @author：zw，微信公共号：yingshengzw
 * @date: 2021/8/28
 * @Copyright：公众号：搬砖暗夜码农 | 博客：https://itzhouwei.com - 沉淀、学习、分享、成长，让自己和他人都能有所收获！
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(value = Exception.class)
    public CommonResponse<String> handlerCommerceException(Exception ex){
        CommonResponse<String> response=new CommonResponse<>(-1,"business error");
        response.setData(ex.getMessage());
        log.error("Commerce service has error [{}]",ex.getMessage(),ex);
        return response;
    }


    @ExceptionHandler(value = RuntimeException.class)
    public CommonResponse<String> handlerCommerceRuntimeException(Exception ex){
        CommonResponse<String> response=new CommonResponse<>(-1,"business error");
        response.setData(ex.getMessage());
        log.error("Commerce service has error [{}]",ex.getMessage(),ex);
        return response;
    }


}
